package com.pig4cloud.mall.product.controller;

import com.pig4cloud.mall.product.dto.BrandParam;
import com.pig4cloud.mall.product.service.BrandService;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 品牌表
 *
 * @author fangyu
 * @date 2020-12-26 23:41:08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/brand")
@Api(value = "brand", tags = "品牌管理")
public class BrandController {

	private final BrandService brandService;

	/**
	 * 添加品牌
	 * {
	 * "bigPic": "",
	 * "brandStory": "",
	 * "factoryStatus": 0,
	 * "firstLetter": "",
	 * "logo": "",
	 * "name": "",
	 * "showStatus": 0,
	 * "sort": 0
	 * }
	 */
	@ApiOperation(value = "添加品牌", notes = "添加品牌")
	@SysLog("添加品牌")
	@PostMapping("/create")
	@PreAuthorize("@pms.hasPermission('product_brand_add')")
	public R create(@RequestBody BrandParam brandParam) {
		int count = brandService.create(brandParam);
		if (count > 0) {
			return R.ok(count);
		} else {
			return R.failed("添加失败!");
		}
	}

	/**
	 * 根据品牌名称分页获取品牌列表
	 */
	@ApiOperation(value = "根据品牌名称分页获取品牌列表", notes = "根据品牌名称分页获取品牌列表")
	@GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('product_brand_get')")
	public R getList(
			@RequestParam(value = "keyword", required = false) String keyword,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
	) {
		return R.ok(brandService.listBrand(keyword, page, pageSize));
	}

	/**
	 * 获取全部品牌列表
	 */
	@ApiOperation(value = "获取全部品牌列表", notes = "获取全部品牌列表")
	@GetMapping("/listAll")
	@PreAuthorize("@pms.hasPermission('product_brand_get')")
	public R listAll() {
		return R.ok(brandService.listAllBrand());
	}

	/**
	 * 根据编号查询品牌信息
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "根据编号查询品牌信息", notes = "根据编号查询品牌信息")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('product_brand_get')")
	public R getBrand(@PathVariable("id") Long id) {
		return R.ok(brandService.getBrand(id));
	}


	/**
	 * 批量删除品牌
	 */
	@ApiOperation(value = "批量删除品牌", notes = "批量删除品牌")
	@SysLog("批量删除品牌")
	@PostMapping("/delete/batch")
	@PreAuthorize("@pms.hasPermission('product_brand_del')")
	public R deleteBatch(@RequestParam("ids") List<Long> ids) {
		int count = brandService.delete(ids);
		if (count > 0) {
			return R.ok(count);
		} else {
			return R.failed("删除失败!");
		}
	}


	/**
	 * 删除品牌
	 */
	@ApiOperation(value = "通过id删除品牌表", notes = "通过id删除品牌表")
	@SysLog("通过id删除品牌表")
	@PostMapping("/delete/{id}")
	@PreAuthorize("@pms.hasPermission('product_brand_del')")
	public R delete(@PathVariable("id") Long id) {
		int count = brandService.delete(id);
		if (count > 0) {
			return R.ok(count);
		} else {
			return R.failed("删除失败!");
		}
	}


	/**
	 * 批量更新厂家制造商状态
	 */
	@ApiOperation(value = "批量更新厂家制造商状态", notes = "批量更新厂家制造商状态")
	@SysLog("批量更新厂家制造商状态")
	@PostMapping("/update/factoryStatus")
	@PreAuthorize("@pms.hasPermission('product_brand_edit')")
	public R updateFactoryStatus(@RequestParam("ids") List<Long> ids, Integer factoryStatus) {
		boolean result = brandService.updateFactoryStatus(ids, factoryStatus);
		if (result) {
			return R.ok(true);
		} else {
			return R.failed("更新失败!");
		}
	}


	/**
	 * 批量更新显示状态
	 */
	@ApiOperation(value = "批量更新显示状态", notes = "批量更新显示状态")
	@SysLog("批量更新显示状态")
	@PostMapping("/update/showStatus")
	@PreAuthorize("@pms.hasPermission('product_brand_edit')")
	public R updateShowStatus(@RequestParam("ids") List<Long> ids, Integer showStatus) {
		boolean result = brandService.updateShowStatus(ids, showStatus);
		if (result) {
			return R.ok(true);
		} else {
			return R.failed("更新失败!");
		}
	}


	/**
	 * 更新品牌
	 */
	@ApiOperation(value = "更新品牌", notes = "更新品牌")
	@SysLog("更新品牌")
	@PostMapping("/update/{id}")
	@PreAuthorize("@pms.hasPermission('product_brand_edit')")
	public R update(@PathVariable("id") Long id, @RequestBody BrandParam brandParam) {
		int count = brandService.update(brandParam, id);
		if (count > 0) {
			return R.ok(count);
		} else {
			return R.failed("更新失败!");
		}
	}

}
